<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="148px">
      <el-form-item label="寄件人姓名" prop="sendName">
        <el-input
          v-model="queryParams.sendName"
          placeholder="请输入寄件人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="寄件人电话" prop="sendPhone">
        <el-input
          v-model="queryParams.sendPhone"
          placeholder="请输入寄件人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="packsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="快递单号" align="center" prop="id"/>
      <el-table-column label="寄送地址" align="center" prop="sendAddress"/>
      <el-table-column label="寄送时间" align="center" prop="sendTime"/>
      <el-table-column label="收货人姓名" align="center" prop="gainName"/>
      <el-table-column label="收货人地址" align="center" prop="gainAddress"/>
      <el-table-column label="收货人电话" align="center" prop="gainPhone"/>
      <!--      <el-table-column label="揽收时间" align="center" prop="collectTime"/>-->
      <!--      <el-table-column label="揽收网点" align="center" prop="collectPoint"/>-->
      <el-table-column label="支付费用" align="center" prop="payTotal"/>
      <el-table-column label="快递状态" align="center" prop="status">

        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === 0"
            type="warning"
            disable-transitions
          >配送中
          </el-tag>
          <el-tag
            v-else-if="scope.row.status === 1"
            type="success"
            disable-transitions
          >正在派件
          </el-tag>
          <el-tag
            v-else
            type="info"
            disable-transitions
          >已完成
          </el-tag>
        </template>

      </el-table-column>
      <el-table-column label="寄件人姓名" align="center" prop="sendName"/>
      <el-table-column label="寄件人电话" align="center" prop="sendPhone"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            v-if="scope.row.status === 1"
            @click="handleView(scope.row)"
          >查看取货码
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTransit(scope.row)"
          >查看物流信息
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleevaluate(scope.row)"
            v-if="scope.row.status==4"
          >评价
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改包裹对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <div v-for="item in logs">
         {{item.sendTime+'--        '+item.sendPoint +'发往' +item.nextPoint }}
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPacks, delPacks, getPacks, listReceivedPacks, updatePacks } from '@/api/boot/packs'
import { listServicepoint } from '@/api/boot/servicepoint'
import { TransitLogs } from '@/api/boot/transit'

export default {
  name: 'Packs',
  data() {
    return {
      logs:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 包裹表格数据
      packsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sendAddress: null,
        sendTime: null,
        gainName: null,
        gainAddress: null,
        gainPhone: null,
        collectTime: null,
        collectPoint: null,
        payTotal: null,
        status: null,
        sendName: null,
        sendPhone: null
      },
      // 表单参数
      form: {},
      // 网点列表
      servicepointList: [],
      // 表单校验
      rules: {
        sendAddress: [
          { required: true, message: '寄送地址不能为空', trigger: 'blur' }
        ],
        sendTime: [
          { required: true, message: '寄送时间不能为空', trigger: 'blur' }
        ],
        gainName: [
          { required: true, message: '收货人姓名不能为空', trigger: 'blur' }
        ],
        gainAddress: [
          { required: true, message: '收货人地址不能为空', trigger: 'blur' }
        ],
        gainPhone: [
          { required: true, message: '收货人联系电话不能为空', trigger: 'blur' }
        ],
        collectTime: [
          { required: true, message: '揽收时间不能为空', trigger: 'blur' }
        ],
        payTotal: [
          { required: true, message: '支付费用不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '快递状态不能为空', trigger: 'change' }
        ],
        sendName: [
          { required: true, message: '寄件人姓名不能为空', trigger: 'blur' }
        ],
        sendPhone: [
          { required: true, message: '寄件人电话不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    listServicepoint(this.queryParams).then(response => {
      this.servicepointList = response.rows

    })
  },
  methods: {
    // 查看取货码
    handleView(row){
      const ids = row.confirmCode || this.ids
      this.$confirm('取货码为："' + ids + '').then(function() {
        // return delPacks(ids)
      })
    },
    handleTransit(row) {
      TransitLogs({fastPackId:row.id}).then(res=>{
        this.open = true
        this.logs = res.rows
        console.log(res)
      })

    }, handleevaluate() {
      this.msgSuccess('评价提交成功')
    },
    /** 查询包裹列表 */
    getList() {
      this.loading = true
      listReceivedPacks(this.queryParams).then(response => {
        this.packsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sendAddress: null,
        sendTime: null,
        gainName: null,
        gainAddress: null,
        gainPhone: null,
        collectTime: null,
        collectPoint: null,
        payTotal: null,
        status: null,
        sendName: null,
        sendPhone: null,
        pointId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加包裹'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPacks(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改包裹'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePacks(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPacks(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除包裹编号为"' + ids + '"的数据项？').then(function() {
        return delPacks(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('boot/packs/export', {
        ...this.queryParams
      }, `packs_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
